home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10758 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  45 lines

  1. Newsgroups: comp.lang.c++
  2. Path: news.sprintlink.net!news1!basmith1
  3. From: basmith@iquest.net (Brian Smith)
  4. Subject: Initializing each element in an array of classes
  5. X-Nntp-Posting-Host: ind-001-236-87.iquest.net
  6. Message-ID: <4hssvm$178_001@basmith1.iquest.net>
  7. Sender: news@iquest.net (News Admin)
  8. Organization: IDT
  9. X-Newsreader: News Xpress Version 1.0 Beta #4
  10. Date: Sat, 9 Mar 1996 21:19:43 GMT
  11.  
  12. I have a Class called Dot that has X and Y among it's data members. It's 
  13. constructor takes the initial values for X and Y as its parameters.
  14.  
  15. class Dot {
  16.   int X;
  17.   int Y;
  18.   Dot(void);
  19.   Dot(int InitX, int InitY, int InitSize = 1) // default for InitSize is 1
  20. };
  21.  
  22. Now I want to create another class Box that has as one of its data members an 
  23. array of 3 Dots, and I want to initialize X and Y for all 3 of the Dots at the 
  24. time class Box is created.
  25.  
  26. class Box {
  27.   int i;
  28.   int j;
  29.   Dot DotArray[3];
  30.   etc...
  31. }
  32.  
  33. The above code compiles, but the 3 Dots in DotArray are constructed using the 
  34. default constructor (the one w/ no parameters). (I know this because I had to 
  35. add it to get it to work.) So now I have 3 Dot objects, but with 
  36. who-knows-what as their X and Y values.
  37.  
  38. How can I create this array of Dots with the X and Y values of, for example, 
  39. (1,1), (2,2), and (3,3)? The Borland Turbo C++ manual is of no help at all.
  40.  
  41. Thanks,
  42.  
  43. Brian Smith
  44.  
  45.